﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 
using Csu.Modsim.ModsimModel; 
using Csu.Modsim.ModsimIO;
using ASquared.ModelOptimization;

namespace iwpm.optimization
{
    class Routing
    {
        private string _path = "";
        private string _base = "";
        private string _outdir = ""; 

        public Routing(string path, string outdir) 
        {
            _path = path;
            _base = Path.GetFileNameWithoutExtension(path);
            _outdir = outdir; 
        }

        public void run()
        {
            string path = Path.Combine(_outdir, _base + "_withRouting.xy");
            File.Copy(_path, path, true);
            runSingle(path, true);

            path = Path.Combine(_outdir, _base + "_withoutRouting.xy");
            File.Copy(_path, path, true);
            runSingle(path, false); 
        }

        private void runSingle(string path, bool routing) 
        {
            // read the model into memory
            Model model = new Model(); 
            XYFileReader.Read(model, path); 

            // build optimodel
            OptiModel optModel = new OptiModel(model, ObjectiveFormulation.HydroTargets, 1e6); 
            model.maxit = 150; 

            // build solver
            ConditionalGradientSolver solver = new ConditionalGradientSolver(optModel);
            solver.Tolerance = 0.001;
            solver.MaxIterations = 150;

            // turn on/off routing 
            ((HydroTargetSeeker)optModel.objective).AddRouting = routing;

            // solve
            solver.Solve();
        }
    }
}
